﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;
namespace ONTS.WEB.Views
{
    public partial class EmployeeKeyIn1 : BaseWebpage
    {
        string Action = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["LOGIN"] != null)
                {
                    initialDDLReligion();
                    initialDDLNationality();
                    OTS_LOGIN model = BaseService.LOGIN;
                    var role = model.ROLE.Split(':');
                    if (role.Contains("administrator"))
                    { 
                        Action = Request.QueryString["Action"].ToString();
                        if (Action == "Edit")
                        {
                            var emp_id = Request.QueryString["id"].ToString();
                            if (!string.IsNullOrEmpty(emp_id))
                            {
                                fillEditForm(emp_id);
                            }
                        }
                        else
                        {
                            clearSession();
                        }
                    }
                }
                else
                {
                    Response.Redirect("/AccessDeniedPage.aspx");
                }

            }
        }

        private void initialDDLReligion()
        {
            ddl_Religion.Items.Clear();
            var religion = new Religion_Service();
            var list = religion.GetAll();
            foreach (var o in list)
            {
                ddl_Religion.Items.Add(o.NAME);
            }
        }

        private void initialDDLNationality() 
        {
            ddl_Nationality.Items.Clear();
            var na = new Nationality_Service();
            var list = na.GetAll();
            foreach (var o in list)
            {
                ddl_Nationality.Items.Add(o.NAME);
            }
        }

        private void clearSession()
        {
            var list = new List<OTS_TIME_SHEET>();
            Session["emp"] = null;
        }

        private void fillEditForm(string emp_id)
        {
            var ds = new Employee_Service(emp_id);
            var emp = ds.GetByEmpId();
            if (emp != null)
            {
                var list = new List<OTS_EMPLOYEE>();
                list.Add(emp);
                Session["emp"] = list;
                ID.Text = emp.ID;
                ID.Enabled = false;
                title.SelectedValue = emp.TITLE;
                NameEN.Text = emp.NAME_EN;
                NameTH.Text = emp.NAME_TH;
                NickName.Text = emp.NICK_NAME;
                Email.Text = emp.EMAIL;
                Gender.SelectedValue = emp.GENDER;
                IDCard.Text = emp.ID_CARD;
                if (emp.DATEOFBIRTH != null && emp.DATEOFBIRTH.Value != DateTime.MinValue)
                {
                    dob.Text = emp.DATEOFBIRTH.Value.ToShortDateString();
                }

                ddl_Nationality.SelectedValue = emp.NATIONALITY;
                ddl_Religion.SelectedValue = emp.RELIGION;
                if (emp.STARTWORK != null && emp.STARTWORK.Value != DateTime.MinValue)
                {
                    StartWork.Text = emp.STARTWORK.Value.ToShortDateString();
                }
                if (emp.ENDWORK != null && emp.ENDWORK.Value != DateTime.MinValue)
                {
                    EndWork.Text = emp.ENDWORK.Value.ToShortDateString();
                }
                Position.Text = emp.POSITION;
                Department.Text = emp.DEPARTMENT;
                Permanent.Text = emp.PERMANENT_ADDRESS;
                Present.Text = emp.PRESENT_ADDRESS;
                HomeTel.Text = emp.HOME;
                MobileTel.Text = emp.MOBILE;
                if (emp.TERMINATED_DATE != null && emp.TERMINATED_DATE.Value != DateTime.MinValue)
                {
                    Terminate.Text = emp.TERMINATED_DATE.Value.ToShortDateString();
                }
                if (emp.PROBATION_DATE != null && emp.PROBATION_DATE.Value != DateTime.MinValue)
                {
                    Probation.Text = emp.PROBATION_DATE.Value.ToShortDateString();
                }
                IQ.Text = emp.IQ_TEST.ToString();
                Most.Text = emp.TYPE_MOS;
                Least.Text = emp.TYPE_LEAST;
                chkWorkStatus.Checked = emp.ONSITE_OFFICE ?? false;
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                var empList = (List<OTS_EMPLOYEE>)HttpContext.Current.Session["emp"];
                if (ID.Text != string.Empty)
                {
                    var empObj = empList.FirstOrDefault();
                    if (empObj == null)
                    {
                        empObj = new OTS_EMPLOYEE();
                    }

                    GetDatafromInput(empObj);


                    if (empObj.Action == ActionEnum.Create)
                    {
                        if (!CheckDuplicateObject(empObj.ID))
                        {
                            var cmd = new Employee_Service(empObj);
                            cmd.Add();
                            ShowSaveSuccessRedirect();
                        }
                        else
                        {
                            ShowAlert("Employee ID : " + empObj.ID + " already in exits");
                        }
                    }
                    else if (empObj.Action == ActionEnum.Update)
                    {
                        var cmd = new Employee_Service(empObj);
                        cmd.Edit();
                        ShowSaveSuccessRedirect();
                    }
                }
                else
                {
                    ShowAlert("กรุณากรอข้อมูล");
                }
            }
            catch (Exception ex)
            {
                ShowAlert("Error : " + ex.Message);
            }
        }

        private bool CheckDuplicateObject(string id)
        {
            var cmd = new Employee_Service();
            var obj = cmd.GetByEmpId(id);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void GetDatafromInput(OTS_EMPLOYEE emp)
        {
            emp.ID = ID.Text;
            emp.TITLE = title.SelectedItem.ToString();
            emp.NAME_EN = NameEN.Text;
            emp.NAME_TH = NameTH.Text;
            emp.NICK_NAME = NickName.Text;
            emp.GENDER = Gender.SelectedItem.Text;
            emp.ID_CARD = IDCard.Text;
            emp.DATEOFBIRTH = Utility.CheckNullableDate(dob.Text);
            emp.NATIONALITY = ddl_Nationality.SelectedValue;
            emp.RELIGION = ddl_Religion.SelectedValue;
            emp.STARTWORK = Utility.CheckNullableDate(StartWork.Text);
            emp.ENDWORK = Utility.CheckNullableDate(EndWork.Text);
            emp.POSITION = Position.Text;
            emp.DEPARTMENT = Department.Text;
            emp.PERMANENT_ADDRESS = Permanent.Text;
            emp.PRESENT_ADDRESS = Present.Text;
            emp.HOME = HomeTel.Text;
            emp.MOBILE = MobileTel.Text;
            emp.TERMINATED_DATE = Utility.CheckNullableDate(Terminate.Text);
            emp.PROBATION_DATE = Utility.CheckNullableDate(Probation.Text);
            emp.IQ_TEST = Utility.CheckInt(IQ.Text);
            emp.TYPE_MOS = Most.Text;
            emp.TYPE_LEAST = Least.Text;
            emp.WORKSTATUS = true;
            emp.EMAIL = Email.Text;
            emp.TYPE = emp_type.SelectedValue;
            emp.ONSITE_OFFICE = chkWorkStatus.Checked;

            Action = Request.QueryString["Action"].ToString();
            if (Action == "Edit")
            {
                emp.Action = ActionEnum.Update;
            }
            else if (Action == "New")
            {
                emp.Action = ActionEnum.Create;
            }
        }
    }
}